import groovy.util.XmlParser
import groovy.xml.MarkupBuilder

includeTargets << grailsScript("Init")

target(updateClasspath: "Builds eclipse classpath"){
	def PATH_SEP = File.separator
	def depends = new XmlParser().parse( new File("/Users/propatalent/.ivy2/cache/org.grails.internal-mud-runtime.xml") );
	def artifacts = depends.dependencies.module.revision.artifacts.artifact
	def IVY_ROOT = "C:\\Users\\propatalent\\.ivy2\\cache";
	
	def markupBuilder = new MarkupBuilder(new FileWriter(".classpath"))
	
	def sortedArtifacts = []
	artifacts.each {
		def location = it.'@location';
		location = location.replace(IVY_ROOT, "IVY")
		sortedArtifacts << location
	}
	sortedArtifacts.sort {
		def i = it.lastIndexOf( PATH_SEP )
		it[i..-1]
	}
	
	markupBuilder.classpath(){
		classpathentry(kind:'src', path:"src/java")
		classpathentry(kind:'src', path:"src/groovy")
		classpathentry(kind:'src', path:"grails-app/conf")
		classpathentry(kind:'src', path:"grails-app/controllers")
		classpathentry(kind:'src', path:"grails-app/domain")
		classpathentry(kind:'src', path:"grails-app/services")
		classpathentry(kind:'src', path:"grails-app/taglib")
		classpathentry(kind:'src', path:"test/integration")
		classpathentry(kind:'src', path:"test/unit")
		classpathentry(kind:'con', path:"org.eclipse.jdt.launching.JRE_CONTAINER")
		classpathentry(kind:'con', path:"org.eclipse.jdt.junit.JUNIT_CONTAINER/4")
		classpathentry(kind:'con', path:"GROOVY_DSL_SUPPORT")
		classpathentry(kind:'con', exported:"true", path:"GROOVY_SUPPORT")
		sortedArtifacts.each {
			classpathentry(kind:'var', path:"${it}")
		}
		classpathentry(kind:'output', path:"web-app/WEB-INF/classes")
	}
}

setDefaultTarget(updateClasspath)